const express=require('express');
const pool=require('../pool.js');
var router=express.Router();
//1.添加预约
router.post('/',(req,res,next)=>{
    var obj=req.body;
	  console.log(res.body)
    var $title=obj.title;
    var $c_count=obj.c_count;
		var $order_time=obj.order_time;
		var $uname=obj.uname;
		var $oname=obj.oname;
		var $ophone=obj.ophone;
		var $ocard=obj.ocard;
		var $price=obj.price;
		var $total=obj.total;
		if(!obj.title){
        res.send({code:401,msg:'title required'});
        return;
    }
    if(!obj.c_count){
        res.send({code:402,msg:'c_count required'});
        return;
    }
		if(!obj.oname){
		    res.send({code:403,msg:'oname required'});
		    return;
		}
		if(!obj.ophone){
		    res.send({code:404,msg:'ophone required'});
		    return;
		}
		if(!obj.ocard){
		    res.send({code:405,msg:'ocard required'});
		    return;
		}
		if(!obj.price){
		    res.send({code:406,msg:'price required'});
		    return;
		}
		if(!obj.total){
		    res.send({code:407,msg:'total required'});
		    return;
		}
		if(!obj.order_time){
		    res.send({code:408,msg:'order_time required'});
		    return;
		}
		if(!obj.uname){
		    res.send({code:409,msg:'uname required'});
		    return;
		}
 
    var  sql=`INSERT INTO xh_order VALUES(NULL,1,${$c_count},"${$order_time}","${$uname}","${$oname}",${$ophone},${$ocard},"${$title}",${$price},${$total})`;
        pool.query(sql,(err,result)=>{
            if(err) {
                next(err);
                return;
            }
            if(result.affectedRows>0){
                res.send({code:200,msg:'add suc'});
            }else{
                res.send({code:500,msg:'add err'});
            }
        });
});;

//2.预约列表
router.get('/list',(req,res,next)=>{
    var $name = req.query.uname;
		if (!$name) {
			res.send({
				code: 401,
				msg: 'uname required'
			});
			return;
		}
		var sql = 'SELECT * FROM xh_order WHERE uname=?'
    pool.query(sql,[$name],(err,result)=>{
        if(err){
            next(err);
            return;
        }
		pool.query(sql)
				res.send({
				 code:200,
				 message: '操作成功',
				 data:result
			 })
		})
});
router.get('/list/img',(req,res,next)=>{
    var $title = req.query.title;
		if (!$title) {
			res.send({
				code: 401,
				msg: 'title required'
			});
			return;
		}
		var sql1 = 'SELECT img FROM xh_detail_pic WHERE title=?'
    pool.query(sql1,[$title],(err,resImag)=>{
        if(err){
            next(err);
            return;
        }
		pool.query(sql1)
				res.send({
				 code:200,
				 message: '操作成功',
				 data:resImag
			 })
		})
});


//3.取消预约
router.delete('/:oid',(req,res)=>{
    var obj=req.params;
    if(!obj.oid){
        res.send({code:401,msg:'oid required'});
        return;
    }
   
    pool.query('DELETE FROM xh_order WHERE oid=?',[obj.oid],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows>0){
            res.send({code:200,msg:'delete succ'});
        }else{
            res.send({code:500,msg:'delete err'});
        }
    });
});

//4.修改预约条目中的预约数量
router.put('/count',(req,res,next)=>{
    var obj=req.body;
    if(!obj.oid){
        res.send({code:401,msg:'oid required'});
        return;
    }
    if(!obj.c_count){
        res.send({code:402,msg:'count required'});
        return;
    }
    if(!req.session.loginUid){
        res.send({code:300,msg:'login required'});
        return;
    }
    pool.query('UPDATE xh_order SET c_count=? WHERE oid=?',[obj.c_count,obj.oid],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        if(result.affectedRows>0){
            res.send({code:200,msg:'update succ'});
        }else{
            res.send({code:500,msg:'update err'});
        }
    });
});

//5.获取当前登录用户的预约内容
router.get('/listchecked',(req,res)=>{
    var output={};
    if(!req.session.loginUid){
        res.send({code:300,msg:'login required'});
        return;
    }
    var sql = "SELECT oid,did,title,c_count,order_time,start FROM xh_detail , xh_order WHERE  o_uid=?";
    pool.query(sql,[req.session.loginUid],(err,result)=>{
        if(err) throw err;
        output.code=200;
        output.data=result[0];
        for(var i=0;i<output.data.length;i++){
            var did=output.data[i].did;
            (function(did,i){
              pool.query('SELECT img FROM xh_scenic WHERE sid=? LIMIT 1',[did],(err,result)=>{
                output.data[i].img=result[0].img;
              });
            })(did,i);
        }
        setTimeout(() => {
            res.send(output);
          }, 100);
    });
});

module.exports=router;